package geektime.spring.springbucks;

import com.github.pagehelper.PageInfo;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.params.PageParam;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Random;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("geektime.spring.springbucks.mapper")
public class SpringBucksApplication implements ApplicationRunner {

	@Autowired
	private CoffeeService coffeeService;

	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		Coffee coffee = Coffee.builder().name("123").price(Money.of(CurrencyUnit.of("CNY"),
				new Random().nextInt(50))).build();
		coffeeService.insert(coffee);
		coffee.setName("234");
		coffeeService.update(coffee);

//		Coffee coffee1 = coffeeService.findById(1L);
//		Coffee coffee2 = coffeeService.findById(1L);
		PageInfo<Coffee> pageInfo = coffeeService.getPageInfo(new PageParam(10, 1));
		log.info("All Coffee: {}", pageInfo);

		coffeeService.deleteAll();


	}
}

